<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Authority extends CI_Model {
	
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	public function getLoginUser($userName,$password){	
		$this->db->where('username',$userName);
		$this->db->where('password',$password);
		$this->db->select('*');
		$query=$this->db->get('user_info');
		return $query->result();
	}
	
	public function getUserInfoById($userId){	
		$this->db->where('id',$userId);
		$this->db->select('*');
		$query=$this->db->get('user_info');
		return $query->result();
	}
	/*
	 * 通过用户的角色ID和角色对应的键值去获取，用户可操作的功能模块
	 * 返回值action为字符串如“INFO,PRODUCT,USERINFO”等模块KEY
	 */
	public function getUserAction($roleId,$roleKey){	
		$this->db->where('id',$roleId);
		$this->db->where('roleKey',$roleKey);
		$this->db->select('action');
		$query=$this->db->get('role_system');
		return $query->result();
	}
	
	/*
	 * 通过模块的KEY去读取对应的模块名
	 */
	public function getModelByKey($modelKey){	
		$this->db->where('modelKey',$modelKey);
		$this->db->select('modelName');
		$query=$this->db->get('model_system');
		return $query->result();
	}
	/*
	 * 通过模块的KEY去读取对应的菜单列表
	 */
	public function getMenuByKey($modelKey){	
		$this->db->where('modelKey',$modelKey);
		$this->db->select('*');
		$query=$this->db->get('menu_system');
		return $query->result();
	}
	
	/**
	 * 根据用户ID获取对应的菜单模块
	 *
	 * @access	public
	 * @param	userId
	 * @return	array
	 */
	function getMenuByUId($userId) {
		$this->load->helper('WebUtil');
		$userInfo=self::getUserInfoById($userId);
		$userAction=self::getUserAction($userInfo[0]->roleId,$userInfo[0]->roleKey);
		$userAction=explode(',',$userAction[0]->action);
		$actionLength=count($userAction);
		$menuList=array();
		for ($index = 0; $index < $actionLength; $index++) {
			$tempList=array();
			$modelName=self::getModelByKey($userAction[$index]);
			$tempList['modelName']=$modelName[0]->modelName;
			$menus=self::getMenuByKey($userAction[$index]);
			$column=array('id','menuName','url');
			$tempList['menus']=formatQueryList($menus,$column);
			array_push($menuList,$tempList);
		}
		return $menuList;
	}
	
	
}

